
package com.x52im.rbchat.logic.chat_root.face;

import android.content.Context;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView.LayoutParams;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.x52im.rbchat.logic.chat_root.model.FaceEntity;

import com.x52im.rbchat.R;

import java.util.ArrayList;
import java.util.List;

/**
 * 表情列表适配器实现类。
 *
 * @author FreeMan
 * @since 7.2
 */
public class EmojiGridAdapter extends BaseAdapter {

    private List<FaceEntity> datas;
    private int mStartIndex;
    private final Context cxt;

    public EmojiGridAdapter(Context cxt, List<FaceEntity> datas, int startIndex) {
        this.cxt = cxt;
        if (datas == null) {
            datas = new ArrayList<FaceEntity>(0);
        }
        this.mStartIndex = startIndex;
        this.datas = datas;
    }

    public void refresh(List<FaceEntity> datas) {
        if (datas == null) {
            datas = new ArrayList<FaceEntity>(0);
        }
        this.datas = datas;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return EmojiPageFragment.ITEM_PAGE_COUNT + 1;
    }

    @Override
    public Object getItem(int position) {
        return datas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    private static class ViewHolder {
        ImageView image;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = new ImageView(cxt);

            // 适配手机屏幕分辨率--------------------------------------------------------START-------
            DisplayMetrics dm = convertView.getContext().getResources().getDisplayMetrics();
            int screenHeight = dm.heightPixels;
            int screeWidth = dm.widthPixels;
            // 设置最大边长为120像素，横向上如有多余空间，系统会以单元格间距均匀填充
            int gridSize = Math.min(120,  screeWidth / EmojiPageFragment.EMOJI_COLUMNS);
            int padding = gridSize / 6;
            System.out.println("表情单元格尺寸(像素), 手机屏幕=" + screenHeight + "*" + screeWidth
                    + ", 单元格边长=" +gridSize +", 单元格内边距="+padding);
            LayoutParams params = new LayoutParams(gridSize, gridSize);
            convertView.setLayoutParams(params);
            convertView.setPadding(padding, padding, padding, padding);

            convertView.setBackgroundResource(R.drawable.chat_face_emoji_bg);
            // 适配手机屏幕分辨率---------------------------------------------------------END--------

            holder.image = (ImageView) convertView;
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        int count = EmojiPageFragment.getDisplayCount();
        int index = mStartIndex + position;
        if (position == EmojiPageFragment.ITEM_PAGE_COUNT || index == count) {
            holder.image.setImageResource(R.drawable.chat_face_emoji_del_btn);
            convertView.setBackgroundDrawable(null);
        } else if (index < count){
            holder.image.setImageDrawable(datas.get(position).getImage());
        }

        return convertView;
    }

}

